<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\conf;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ConfController extends Controller
{
    /**
     * Display a listing of the resource.
     * | GET|HEAD| Admin/conf | Admin.article.index
     * @return \Illuminate\Http\Response
     *  显示导航类表
     */
    public function index()
    {
        $data = Conf::orderBy('conf_order','asc')->paginate(10);
        foreach($data as $k => $v)
        {
            switch($v->conf_type)
            {
                case 'input':
                    $v->_html = "<input type='text' class='lg' name='conf_content[]' value='$v->conf_content'>";
                break;
                case 'textarea':
                    $v->_html = "<textarea name='conf_content[]'>".$v->conf_content."</textarea>";
                break;
                case 'radio':
                    $arr = explode(',',$v->conf_value);
                    $str = '';
                   foreach($arr as $m=>$n)
                   {
                       $r = explode('|',$n);
                       $c = $r[0] == $v->conf_content ? ' checked ':'';
                       $str .= "<input type='radio' name='conf_content[]' ".$c." value='".$r[0]."'>".$r[1]."&nbsp;&nbsp;";
                   }
                    $v->_html = $str;
                break;
            }
        }
        return view('Admin.conf.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     * | GET|HEAD| Admin/article/create | Admin.article.create
     * @return \Illuminate\Http\Response
     * 显示文章添加页面
     */
    public function create()
    {
        return view('Admin.conf.add');
    }

    /**
     * Store a newly created resource in storage.
     * | POST| Admin/articleAdmin/article| Admin.article.store|
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * 执行文章添加
     */
    public function store(Request $request)
    {
        $input = Input::except('_token');
        $rules = [
            'conf_name'=>'required',
            'conf_title'=>'required',
        ];
        $message = [
            'conf_name.required'=>'配置项名称不能为空',
            'conf_title.required'=>'配置项标题不能为空',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator -> passes())
        {
            $res = Conf::create($input);
            if ($res) {
                return redirect('Admin/conf')->with('info', '配置项添加成功');
            } else {
                return back()->with('info', '数据添加失败，请稍后再试');
            }
        }else
        {
            return back()->withErrors($validator);
        }
    }

    /**
     * Show the form for editing the specified resource.
     * | GET|HEAD| Admin/article/{article}/edit| Admin.article.edit
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 显示文章修改页面
     */
    public function edit($id)
    {
        $data = Conf::find($id);
        return view('Admin.conf.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     * | PUT|PATCH | Admin/article/{article} | Admin.article.update
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 执行文章更新
     */
    public function update(Request $request, $id)
    {
        $input = Input::except('_token','_method');
        $res = Conf::where('conf_id',$id)->update($input);
        if($res)
        {
            $this->putFile();
            return redirect('Admin/conf')->with('info','配置项更新成功');
        }else
        {
            return back()->with('info','配置项更新失败，请稍候再试');
        }
    }

    /**
     * Remove the specified resource from storage.
     * | DELETE | Admin/article/{article} | Admin.article.destroy
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 执行文章删除
     */
    public function destroy($id)
    {
        $res = Conf::where('conf_id',$id)->delete();
        if($res)
        {
            $this->putFile();
            $data = [
                'status'=>0,
                'msg'=>'分类删除成功',
            ];
        }else
        {
            $data = [
                'status'=>1,
                'msg'=>'分类删除失败，请稍候再试',
            ];
        }
        return $data;
    }

    public function changeorder()
    {
        $input = Input::all();
        $conf = Conf::find($input['conf_id']);
        $conf -> conf_order = $input['conf_order'];
        $res = $conf->update();
        if($res)
        {
            $date = [
                'status' => 0,
                'msg' => '配置项排序更新成功',
            ];
        }else
        {
            $date = [
                'status' => 1,
                'msg' => '导航更新失败，请稍候再试',
            ];
        }
        return $date;
    }

    public function changeContent()
    {
        $input = Input::all();
        foreach($input['conf_id'] as $k=>$v)
        {
            Conf::where('conf_id',$v)->update(['conf_content' => $input['conf_content'][$k]]);
        }
        $this->putFile();
        return back()->with(['info'=>'配置项更新成功']);
    }

    public function putFile()
    {
        $conf = Conf::pluck('conf_content','conf_title')->all();
        $path =base_path().'/config/web.php';
        $str = '<?php return '.var_export($conf,true).';';
        file_put_contents($path,$str);
    }
}
